/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hibernate;

import java.util.Date;

import org.hibernate.Session;
import photogallery.Comment;
import photogallery.HibernateUtil;
import photogallery.Image;

/**
 *
 * @author dinhnb60088
 */
public class CommentDAO {
    private Session session;
    public CommentDAO(){
        session = HibernateUtil.getSessionFactory().openSession();
    }
    public void insertComment(Integer commentId, String author, Date createdDate, String content, String imageId){
        try{
            session.getTransaction().begin();
            ImageDAO img = (ImageDAO) session.get(ImageDAO.class, imageId);
            Comment comment = new Comment(commentId, author, createdDate, content);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

     public void deleteComment(Integer commentId) {
        try {
            session.getTransaction().begin();
            Comment comment = (Comment) session.get(Comment.class, commentId);
            session.delete(comment);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    public  Comment getComment(Integer imageId) {
        try{
            session.getTransaction().begin();
            Comment comment = (Comment) session.get(Comment.class, imageId);
            session.flush();
            session.getTransaction().commit();
            return comment;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
    }
        return null;
   }
}
